using UnityEngine;
using System.Collections;

public class Destroyer : Player
{
	SphereCollider sc;
	Rigidbody rb;
	private float speedUpTime;
	private float slowDownTime;

    public AudioClip audioSnakeEats;
    public AudioClip audioSpeedUp;
    public AudioClip audioSlowDown;

	public Destroyer()
	{
		movementSpeed = new AffectableSpeed(false);
		rotationSpeed = new AffectableSpeed(false);
		walkAnimationName = "destroyeranimatedversion";
	}
	
	public override void Update()
	{
		handlePowerUps(Time.deltaTime);
		if(getCommandSource().consumePrimarySkill())
			Debug.Log("Destroyer farted");
		base.Update();
        audio.PlayOneShot(audioSnakeEats);
	}
	
	public override void reset()
	{
		
	}
	
	void OnTriggerEnter(Collider other) {
		Debug.Log("collision");
		if(other.gameObject.GetComponent<SimpleBlock>() != null){
			Debug.Log("Box destroyed");
            audio.PlayOneShot(audioSnakeEats);
			killEgg(other.gameObject);
		}
		else if(other.gameObject.GetComponent<Builder>() != null){
			Builder b = (Builder)other.gameObject.GetComponent<Builder>();
			if(b.isEatable()){
				b.setEaten(true);
			}
		}
	}
	
	private void killEgg(GameObject obj){
		setItem(Item.SlowDown);
		Debug.Log("collected SLOW_DOWN item");
		((Builder)GameObject.Find("Builder").GetComponent("Builder")).removeEgg(obj);
		Destroy(obj);
	}
	
	//-----------------------------------Power Ups-----------------------------------------
	
	private void handlePowerUps(float deltaTime){
		speedUpTime = Mathf.Max(0, speedUpTime-deltaTime);
		slowDownTime = Mathf.Max(0, slowDownTime-deltaTime);
		
		Debug.Log("ttttime: "+speedUpTime);
		
		if(speedUpTime == 0){
			Debug.Log("speedUp over");
			movementSpeed.disableGoodEffect();
		}
		if(slowDownTime == 0){
			Debug.Log("slowDown over");
			movementSpeed.disableBadEffect();
		}
	}
	
	public override void speedUp(){
		Debug.Log("speedUp activated");
		movementSpeed.enableGoodEffect();
		speedUpTime = Balance.SPEED_UP_TIME;
        audio.PlayOneShot(audioSpeedUp);
	}
	
	public override void slowDown(){
		((Builder)GameObject.Find("Builder").GetComponent<Builder>()).getSlowedDown();
        audio.PlayOneShot(audioSlowDown);
    }
	
	public void getSlowedDown(){
		Debug.Log("slowDown activated");
		movementSpeed.enableBadEffect();
		slowDownTime = Balance.SLOW_DOWN_TIME;
	}
	
	public override void multify(){
		
	}
	
}
